"use strict";
var date_format = function (timestamp, format) {
   format = format || 'yyyy年MM月dd';
   timestamp = timestamp + "";
   if (timestamp * 1 > 0 && timestamp.length == 10) {
      timestamp = timestamp * 1000;
   }

   // 通过getDate()方法获取date类型的时间
   var regYear = new RegExp("(y+)", "i");
   var realDate = new Date(timestamp);

   function timeFormat(num) {
      return num < 10 ? '0' + num : num;
   }

   var date = [
      ["M+", timeFormat(realDate.getMonth() + 1)],
      ["d+", timeFormat(realDate.getDate())],
      ["h+", timeFormat(realDate.getHours())],
      ["m+", timeFormat(realDate.getMinutes())],
      ["s+", timeFormat(realDate.getSeconds())],
      ["q+", Math.floor((realDate.getMonth() + 3) / 3)],
      ["S+", realDate.getMilliseconds()],
   ];
   var reg1 = regYear.exec(format);

   if (reg1) {
      format = format.replace(reg1[1], (realDate.getFullYear() + '').substring(4 - reg1[1].length));
   }
   for (var i = 0; i < date.length; i++) {
      var k = date[i][0];
      var v = date[i][1];
      // getRegExp初始化一个正则表达式对象
      var reg2 = new RegExp("(" + k + ")").exec(format);
      if (reg2) {
         format = format.replace(reg2[1], reg2[1].length == 1
            ? v : ("00" + v).substring(("" + v).length));
      }
   }
   return format;
};
layui.define(["layer", "okMock"], function (exprots) {
   var $ = layui.jquery;
   var okMock= layui.okMock;
   var okUtils = {
      /**
       * 获取body的总宽度
       */
      getBodyWidth: function () {
         return document.body.scrollWidth;
      },
      /**
       * 主要用于对ECharts视图自动适应宽度
       */
      echartsResize: function (element) {
         var element = element || [];
         window.addEventListener("resize", function () {
            var isResize = localStorage.getItem("isResize");
            // if (isResize == "false") {
            for (let i = 0; i < element.length; i++) {
               element[i].resize();
            }
            // }
         });
      },
      /**
       * ajax()函数二次封装
       * @param url
       * @param type
       * @param params
       * @param load
       * @returns {*|never|{always, promise, state, then}}
       */
      ajax: function (url, type, params, load) {
         var deferred = $.Deferred();
         var loadIndex;
         $.ajax({
            url: url,
            type: type || "get",
            data: params || {},
            dataType: "json",
            async: JSON.stringify(arguments).indexOf('async') !== -1 ? false : true, // 判断所传参数是否包含'async'字段,则包含则为同步
            headers: {
               'token': okUtils.get_token(),
            },
            beforeSend: function () {
               if (load) {
                  loadIndex = layer.load(0, {shade: 0.3});
               }
            },
            success: function (data) {
               deferred.resolve(data);
               //只有token验证为空时才会返回code=2，此时需要重新登录
               if (data.code == 2) {
                  layer.msg(data.message, { icon: 2, time: 2000 }, function () {
                     //重定向到登录页面
                     top.window.location = "/pages/login.html";
                  });
               }
            },
            complete: function () {
               if (load) {
                  layer.close(loadIndex);
               }
            },
            error: function () {
               layer.close(loadIndex);
               layer.msg("服务器错误", {icon: 2, time: 2000});
               deferred.reject("okUtils.ajax error: 服务器错误");
            }
         });
         return deferred.promise();
      },
      /**
       * 主要用于针对表格批量操作操作之前的检查
       * @param table
       * @returns {string}
       */
      tableBatchCheck: function (table) {
         var checkStatus = table.checkStatus("tableId");
         var rows = checkStatus.data.length;
         if (rows > 0) {
            var idsStr = "";
            for (var i = 0; i < checkStatus.data.length; i++) {
               idsStr += checkStatus.data[i].id + ",";
            }
            return idsStr;
         } else {
            layer.msg("未选择有效数据", {offset: "t", anim: 6});
         }
      },
      /**
       * 在表格页面操作成功后弹窗提示
       * @param content
       */
      tableSuccessMsg: function (content) {
         layer.msg(content, {icon: 1, time: 1000}, function () {
            // 刷新当前页table数据
            $(".layui-laypage-btn")[0].click();
         });
      },
      /**
       * sessionStorage 二次封装
       */
      session: function (name, value) {
         if (value) { /**设置*/
            if (typeof value == "object") {
               sessionStorage.setItem(name, JSON.stringify(value));
            } else {
               sessionStorage.setItem(name, value);
            }
         } else if (null !== value) {
            /**获取*/
            let val = sessionStorage.getItem(name);
            try {
               val = JSON.parse(val);
               return val;
            } catch (err) {
               return val;
            }
         } else { /**清除*/
            return sessionStorage.removeItem(name);
         }
      },
      /**
       * localStorage 二次封装
       */
      local: function (name, value) {
         if (value) { /**设置*/
            if (typeof value == "object") {
               localStorage.setItem(name, JSON.stringify(value));
            } else {
               localStorage.setItem(name, value);
            }
         } else if (null !== value) {
            /**获取*/
            let val = localStorage.getItem(name);
            try {
               val = JSON.parse(val);
               return val;
            } catch (err) {
               return val;
            }
         } else { /**清除*/
            return localStorage.removeItem(name);
         }
      },
      /**
       * 获取父窗体的okTab
       * @returns {string}
       */
      getOkTab: function () {
         return parent.objOkTab;
      },
      /**
       * 格式化当前日期
       * @param date
       * @param fmt
       * @returns {void | string}
       */
      dateFormat: function (date, fmt) {
         date = date || new Date();
         fmt = fmt || "yyyy年M月s日";
         var o = {
            "M+": date.getMonth() + 1,
            "d+": date.getDate(),
            "h+": date.getHours(),
            "m+": date.getMinutes(),
            "s+": date.getSeconds(),
            "q+": Math.floor((date.getMonth() + 3) / 3),
            "S": date.getMilliseconds()
         };
         if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
         for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
               fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
         return fmt;
      },
      number: {
         /**
          * 判断是否为一个正常的数字
          * @param num
          */
         isNumber: function (num) {
            if (num && !isNaN(num)) {
               return true;
            }
            return false;
         },
         /**
          * 判断一个数字是否包括在某个范围
          * @param num
          * @param begin
          * @param end
          */
         isNumberWith: function (num, begin, end) {
            if (this.isNumber(num)) {
               if (num >= begin && num <= end) {
                  return true;
               }
               return false;
            }
         },
      },


      //获取路径传过来的ID
      getUrlParams: function (name) { // 不传name返回所有值，否则返回对应值
         var url = window.location.search;//?id=13&pid=88
         if (url.indexOf('?') == -1) {
            return false;
         }
         url = url.substr(1);
         url = url.split('&');
         var name = name || '';
         var nameres;
         // 获取全部参数及其值
         for (var i = 0; i < url.length; i++) {
            var info = url[i].split('=');
            var obj = {};
            obj[info[0]] = decodeURI(info[1]);
            url[i] = obj;
         }
         // 如果传入一个参数名称，就匹配其值
         if (name) {
            for (var i = 0; i < url.length; i++) {
               for (const key in url[i]) {
                  if (key == name) {
                     nameres = url[i][key];
                  }
               }
            }
         } else {
            nameres = url;
         }
         // 返回结果
         return nameres;
      },

      //获取页面token
      get_token: function () {
         return window.localStorage.getItem('token');
      },

      //根据页面路径获取动作权限
      action_data: function (purl) {
         var url = purl || window.location.pathname;
         var str = url.slice(1),
            res = ''
         okUtils.ajax(okMock.api.menu.action_by_path, "get", {
            path: str
         }, true, 'async').done(function (response) {
            if (response.code == HTTP_OK) {
               res = response.data
            }
         }).fail(function (error) {
            res = error
         });
         return res
      },
      
      //根据权限动态创建添加/修改/删除...按钮
      action_show: function (action, parm) {
         var parm = parm || {};
         $("#toolbarTpl").append("<div class=\"layui-btn-container\"></div>");
         $.each(action, function (idx, item) {
            switch (item) {
               case '1':
                  //添加
                  var add_btn_name = parm.add_btn_name || "添加";
                  $("#toolbarTpl").find('.layui-btn-container').append(
                     "<button class=\'layui-btn layui-btn-normal  layui-btn-sm\' lay-event=\'add\'>" + add_btn_name + "</button>"
                  );
                  break;
               case '2':
                  //删除
                  $("#operationTpl").append(
                     "<a href=\"javascript:\" title=\"删除\" lay-event=\"del\"><i class=\"layui-icon\">&#xe640;</i></a>"
                  );
                  break;
               case '3':
                  //判断禁用/启用按钮是否可见
                  if (parm.status_btn_is_display) {
                     var eleJY =
                        ' <span class="layui-btn layui-btn-danger layui-btn-xs" lay-event="prohibit">禁用</span>'
                     var eleQY =
                        '<span class="layui-btn layui-btn-normal layui-btn-xs" lay-event="enable">启用</span>'
                     var s1 = ' {{#  if(d.status == 1){ }}'
                     var s2 = '{{#  } else if(d.status == -1 ) { }}'
                     var s3 = ' {{#  } }}          '
                     $("#operationTpl").append(`${s1}${eleJY}${s2}${eleQY}${s3}`)
                  }
                  //修改
                  $("#operationTpl").prepend(
                     "<a href=\"javascript:\" title=\"编辑\" lay-event=\"edit\"><i class=\"layui-icon\">&#xe642;</i></a>"
                  );
                  break;
            }
         })
      },

   };
   exprots("okUtils", okUtils);
});
